package menu_repo

import (
	"context"
	"vmapp/app0/internal/biz/repo"
	"vmapp/app0/internal/conf"
	"vmapp/app0/internal/data/base"
	"vmapp/app0/internal/models"

	"github.com/go-kratos/kratos/v2/log"
)

type menuRepo struct {
	 *base.Data
	log  *log.Helper
}

func NewMenuRepo(data *base.Data, component *conf.BootComponent) repo.MenuRepo {
	return &menuRepo{
		Data: data,
		log:  component.Logger,
	}
}

func (t *menuRepo) CreateMenu(ctx context.Context, one *models.Menu) error {
	return t.DB(ctx).Create(&one).Error
}

func (t *menuRepo) UpdateMenu(ctx context.Context, one *models.Menu) error {
	return t.DB(ctx).Model(new(models.Menu)).Select("*").Where("id = ?", one.Id).UpdateColumns(one).Error
}

func (t *menuRepo) DeleteMenu(ctx context.Context, id int32) error {
	return t.DB(ctx).Delete(new(models.Menu), "id = ?", id).Error
}

func (t *menuRepo) GetMenu(ctx context.Context, id int32) (one *models.Menu, err error) {
	err = t.DB(ctx).First(&one, "id = ?", id).Error
	return
}

func (t *menuRepo) ListMenu(ctx context.Context) (all []*models.Menu, err error) {
	err = t.DB(ctx).Model(new(models.Menu)).Order("sort desc").Find(&all).Error
	return
}

func (t *menuRepo) GetMenuByIds(ctx context.Context, ids []string) (all []*models.Menu, err error) {
	err = t.DB(ctx).Model(new(models.Menu)).Where("id in (?)", ids).Order("sort desc").Find(&all).Error
	return
}

func (t *menuRepo) GetMenuByPid(ctx context.Context, pid int32) (all []*models.Menu, err error) {
	err = t.DB(ctx).Find(&all, "pid = ?", pid).Error
	return
}
